import { Controller } from "../../mvc/core/Controller";
import { ITreasureBoxController } from "./interfaces/ITreasureBoxController";
import { IView } from "../../mvc/interfaces/IView";
import { TreasureBoxModule } from "../../../modules/TreasureBoxModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { ITreasureBoxModule } from "../../../modules/interfaces/ITreasureBoxModule";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SOpenTreasureBoxReturn } from "../../../Message/Public/SOpenTreasureBoxReturn";
import { TreasureBoxItemWindow } from "../view/treasureBox/TreasureBoxItemWindow";
type int = number;
//class TreasureBoxController
    
    export  class TreasureBoxController extends Controller implements ITreasureBoxController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._treasureBox == null) 
            {
                this._treasureBox = new TreasureBoxModule();
            }
            return this._treasureBox;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.TreasureBoxPropUsedOpen, this.onTreasureBoxPropUsedOpenHandler);
            Dispatcher.addEventListener(EventName.TreasureBoxPropUsed, this.onTreasureBoxPropUsedHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateTreasureBoxReturn, this.onTreasureBoxReturnHandler);
            return;
        }

        /* internal  */onTreasureBoxPropUsedOpenHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data as ItemData;
            if (loc1) 
            {
                if (loc1.category == ECategory._ECategoryProp && loc1.type == EProp._EPropTreasureBoxKey) 
                {
                    loc2 = cache.pack.backPackCache.getItemByCode(loc1.effectEx);
                    if (loc2 == null && !(loc1.effectEx == 0)) 
                    {
                        loc2 = cache.pack.backPackCache.getItemByCode(new ItemData(loc1.effectEx).itemInfo.item.codeUnbind);
                    }
                    loc1 = loc2;
                }
                if (loc1) 
                {
                    view.show();
                    (view as ITreasureBoxModule).updateItemData(loc1);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(30319));
                }
            }
            return;
        }

        /* internal  */onTreasureBoxPropUsedHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.uid;
            var loc3=loc1.amount;
            var loc4=loc1.useBind;
            var loc5=loc1.autoBuy;
            this._code = loc1.code;
            GameProxy.packProxy.useItem(loc2, loc3, [loc4.toString(), loc5.toString()]);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object): void
        {
            if (this._treasureBox && !this._treasureBox.isHide) 
            {
                this._treasureBox.updatePropNum();
            }
            return;
        }

        /* internal  */onTreasureBoxReturnHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SOpenTreasureBoxReturn;
            if (loc1) 
            {
                if (this._itemWindow == null) 
                {
                    this._itemWindow = new TreasureBoxItemWindow();
                }
                if (this._itemWindow.isHide) 
                {
                    this._itemWindow.show();
                }
                this._code = loc1.code;
                this._itemWindow.updateItems(loc1, this._code);
            }
            return;
        }

        private /* var */_treasureBox: ITreasureBoxModule;

        private /* var */_code: int;

        private /* var */_itemWindow: TreasureBoxItemWindow;
    }
